{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "![](https://raw.githubusercontent.com/Qinbf/tf-model-zoo/master/README_IMG/01.jpg)\n",
    "AI MOOC： **www.ai-xlab.com**  \n",
    "如果你也是AI爱好者，可以添加我的微信一起交流：**sdxxqbf**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn import preprocessing\n",
    "# 数据是否需要标准化\n",
    "scale = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "LR-testSet.csv not found.",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mOSError\u001B[0m                                   Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-4-aa031662ca2b>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[1;31m# 载入数据\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 2\u001B[1;33m \u001B[0mdata\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mnp\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mgenfromtxt\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"LR-testSet.csv\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mdelimiter\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m\",\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      3\u001B[0m \u001B[0mx_data\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mdata\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m-\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[0my_data\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mdata\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m-\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\numpy\\lib\\npyio.py\u001B[0m in \u001B[0;36mgenfromtxt\u001B[1;34m(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding)\u001B[0m\n\u001B[0;32m   1770\u001B[0m             \u001B[0mfname\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mos_fspath\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfname\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1771\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0misinstance\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfname\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mbasestring\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1772\u001B[1;33m             \u001B[0mfid\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mnp\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mlib\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_datasource\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfname\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'rt'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mencoding\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mencoding\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1773\u001B[0m             \u001B[0mfid_ctx\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mcontextlib\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mclosing\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfid\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1774\u001B[0m         \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\numpy\\lib\\_datasource.py\u001B[0m in \u001B[0;36mopen\u001B[1;34m(path, mode, destpath, encoding, newline)\u001B[0m\n\u001B[0;32m    267\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    268\u001B[0m     \u001B[0mds\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mDataSource\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdestpath\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 269\u001B[1;33m     \u001B[1;32mreturn\u001B[0m \u001B[0mds\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mencoding\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mencoding\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mnewline\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mnewline\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    270\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    271\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\numpy\\lib\\_datasource.py\u001B[0m in \u001B[0;36mopen\u001B[1;34m(self, path, mode, encoding, newline)\u001B[0m\n\u001B[0;32m    621\u001B[0m                                       encoding=encoding, newline=newline)\n\u001B[0;32m    622\u001B[0m         \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 623\u001B[1;33m             \u001B[1;32mraise\u001B[0m \u001B[0mIOError\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"%s not found.\"\u001B[0m \u001B[1;33m%\u001B[0m \u001B[0mpath\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    624\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    625\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mOSError\u001B[0m: LR-testSet.csv not found."
     ]
    }
   ],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"LR-testSet.csv\", delimiter=\",\")\n",
    "x_data = data[:,:-1]\n",
    "y_data = data[:,-1]\n",
    "    \n",
    "def plot():\n",
    "    x0 = []\n",
    "    x1 = []\n",
    "    y0 = []\n",
    "    y1 = []\n",
    "    # 切分不同类别的数据\n",
    "    for i in range(len(x_data)):\n",
    "        if y_data[i]==0:\n",
    "            x0.append(x_data[i,0])\n",
    "            y0.append(x_data[i,1])\n",
    "        else:\n",
    "            x1.append(x_data[i,0])\n",
    "            y1.append(x_data[i,1])\n",
    "\n",
    "    # 画图\n",
    "    scatter0 = plt.scatter(x0, y0, c='b', marker='o')\n",
    "    scatter1 = plt.scatter(x1, y1, c='r', marker='x')\n",
    "    #画图例\n",
    "    plt.legend(handles=[scatter0,scatter1],labels=['label0','label1'],loc='best')\n",
    "    \n",
    "plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据处理，添加偏置项\n",
    "x_data = data[:,:-1]\n",
    "y_data = data[:,-1,np.newaxis]\n",
    "\n",
    "print(np.mat(x_data).shape)\n",
    "print(np.mat(y_data).shape)\n",
    "# 给样本添加偏置项\n",
    "X_data = np.concatenate((np.ones((100,1)),x_data),axis=1)\n",
    "print(X_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    return 1.0/(1+np.exp(-x))\n",
    "\n",
    "def cost(xMat, yMat, ws):\n",
    "    left = np.multiply(yMat, np.log(sigmoid(xMat*ws)))\n",
    "    right = np.multiply(1 - yMat, np.log(1 - sigmoid(xMat*ws)))\n",
    "    return np.sum(left + right) / -(len(xMat))\n",
    "\n",
    "def gradAscent(xArr, yArr):\n",
    "    \n",
    "    if scale == True:\n",
    "        xArr = preprocessing.scale(xArr)\n",
    "    xMat = np.mat(xArr)\n",
    "    yMat = np.mat(yArr)\n",
    "    \n",
    "    lr = 0.001\n",
    "    epochs = 10000\n",
    "    costList = []\n",
    "    # 计算数据行列数\n",
    "    # 行代表数据个数，列代表权值个数\n",
    "    m,n = np.shape(xMat)\n",
    "    # 初始化权值\n",
    "    ws = np.mat(np.ones((n,1)))\n",
    "    \n",
    "    for i in range(epochs+1):             \n",
    "        # xMat和weights矩阵相乘\n",
    "        h = sigmoid(xMat*ws)   \n",
    "        # 计算误差\n",
    "        ws_grad = xMat.T*(h - yMat)/m\n",
    "        ws = ws - lr*ws_grad \n",
    "        \n",
    "        if i % 50 == 0:\n",
    "            costList.append(cost(xMat,yMat,ws))\n",
    "    return ws,costList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练模型，得到权值和cost值的变化\n",
    "ws,costList = gradAscent(X_data, y_data)\n",
    "print(ws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if scale == False:\n",
    "    # 画图决策边界\n",
    "    plot()\n",
    "    x_test = [[-4],[3]]\n",
    "    y_test = (-ws[0] - x_test*ws[1])/ws[2]\n",
    "    plt.plot(x_test, y_test, 'k')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 画图 loss值的变化\n",
    "x = np.linspace(0,10000,201)\n",
    "plt.plot(x, costList, c='r')\n",
    "plt.title('Train')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Cost')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预测\n",
    "def predict(x_data, ws):\n",
    "    if scale == True:\n",
    "        x_data = preprocessing.scale(x_data)\n",
    "    xMat = np.mat(x_data)\n",
    "    ws = np.mat(ws)\n",
    "    return [1 if x >= 0.5 else 0 for x in sigmoid(xMat*ws)]\n",
    "\n",
    "predictions = predict(X_data, ws)\n",
    "\n",
    "print(classification_report(y_data, predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}